
var AppealOrgList = function() {

	var left_margin_step = 15;
	var offsetTop=0;
	if($("#offsetTop").val()!=0){
		offsetTop=$("#offsetTop").val();
	}
	$(".ui-drop_dialog_content").attr("style","height:400px");
	$("#qryInvoiceNo").bind(
			'click',
			function() {
			
			if($("#qryInvoiceNo").val()!=null&&$("#qryInvoiceNo").val()!=""&&$("#qryInvoiceNo").val()!=" *"){
					if($("#complainHeadPicker").find("[name='pk_purOrg_resp']").length==0){
						var orgStr=$("#qryInvoiceNo").val();
						var pk_purInvoice=$("#pk_purInvoice").val();
						var strTemp=orgStr.split(",");
						var pkTemp=pk_purInvoice.split(",");
						for(var i=0;i<pkTemp.length;i++){				    
	                       var checkBOX = $("<input/>")
									.attr("type", "checkbox")
								    .attr("id",pkTemp[i]+strTemp[i])
									.attr("name", "pk_purOrg_resp")
									.val(pkTemp[i]+strTemp[i])
									.attr("style","margin-left:0px;margin-top:0px").attr("checked","checked");		
	                       var labelProd = $("<label />").html(strTemp[i])
						   var div = $("<div></div>").attr("id", pkTemp[i]).attr("name", pkTemp[i]).attr("style", "float:left;display:inline").append(checkBOX).append(labelProd);						   						   
						   $("#sel_orgc").append(div);
						   			                
	                       $(checkBOX).click(function(){	                     
	                       	   var checkValue = $(this).val();
							   var checkOrgPk = checkValue.substr(0,20);
							   var checkOrgName = checkValue.substr(21);
	                       	   $(this).parent().remove();
	                       	   var divHeight = $("#sel_orgc")[0].offsetHeight;
							   var allChild = $("#sel_orgc").children();
							   var lastChildHeight = allChild[allChild.length - 1].offsetTop;
								//璁剧疆宸查€変骇鍝佸幓楂樺害
								var objDialogs = $(".ui-drop_dialog_content");
								var idxObj;
								for(idxObj=0;idxObj<objDialogs.length;idxObj++){
									var objDialog = $(objDialogs[idxObj]);
									if(objDialog.is(":visible")){
										objDialog.attr("style","height:"+ lastChildHeight +"px");
										break;
									}
								}
								$($(".border.lft")[idxObj]).attr("style","height:"+ lastChildHeight +"px");
								$($(".border.rit")[idxObj]).attr("style","height:"+ lastChildHeight +"px");
								$(".ui-drop_dialog_content").attr("style","height:"+ lastChildHeight +"px");
								$(".border.lft").attr("style","height:"+ lastChildHeight +"px");
								$(".border.rit").attr("style","height:"+ lastChildHeight +"px");
								$("#sel_orgc").attr("style","height:"+ lastChildHeight +"px");
	                       })                      
						}					   	
					   var lastChildHeight = $("#offsetTop").val();											
					   $(".ui-drop_dialog_content").attr("style","height:"+ lastChildHeight +"px");
		               $(".border.lft").attr("style","height:"+ lastChildHeight +"px");
		               $(".border.rit").attr("style","height:"+ lastChildHeight +"px");
		               $("#sel_orgc").attr("style","height:"+ lastChildHeight +"px");
					}
				}
				var myts = new Date();
				var formStr = $(this).parents("form").find("#org_filterc").serialize();
				jQuery.ajax( {
					type : "POST",
					async : false,
					url : "/ebvp/advorappcoll/complainorgall?myts=" + myts,
					dataType : "text",
					data : "&" + formStr,
					success : function(result) {
						if(result != "[]"){
							var orgArr = null;
							if(result != "" && result != null){
								orgArr =eval(result);
								
								$("#complainHeadPicker").find(".tree_list12").empty();
								for ( var loop = 0; loop < orgArr.length; loop++) {
									var obj = orgArr[loop];									
									var inputObj = $("<input/>")
												.attr("type", "checkbox")
												.attr("id", "pk_purOrg_intree")
												.attr("name", "pk_purOrg_intree")
												.val(obj.pk_complainttype+"_"+obj.vname)
												.addClass(obj.pk_complainttype);	
									$(inputObj).click(function(){										
										var checkValue = $(this).val();
										var checkOrgPk = checkValue.substr(0,20);
										var checkOrgName = checkValue.substr(21);										
										if(this.checked){
										   var tempLength=0;									  
										   var checkBox = $("<input/>")
														.attr("type", "checkbox")
														.attr("name", "pk_purOrg_resp")
														.val(checkValue)
														.attr("style","margin-left:0px;margin-top:0px").attr("checked","checked");
										   $(checkBox).click(function(){
												var checkValue = $(this).val();
												var checkOrgPk = checkValue.substr(0,20);
												var checkOrgName = checkValue.substr(21);
												var objs = $("."+checkOrgPk);
												if(objs.length > 0){
													objs[0].checked = false;
												}
												$(this).parent().remove();										
												var divHeight = $("#sel_orgc")[0].offsetHeight;
												var allChild = $("#sel_orgc").children();
												var lastChildHeight = allChild[allChild.length - 1].offsetTop;
												offsetTop=lastChildHeight;		
												//璁剧疆宸查€変骇鍝佸幓楂樺害
												var objDialogs = $(".ui-drop_dialog_content");
												var idxObj;
												for(idxObj=0;idxObj<objDialogs.length;idxObj++){
													var objDialog = $(objDialogs[idxObj]);
													if(objDialog.is(":visible")){
														objDialog.attr("style","height:"+ lastChildHeight +"px");
														break;
													}
												}
												$($(".border.lft")[idxObj]).attr("style","height:"+ lastChildHeight +"px");
												$($(".border.rit")[idxObj]).attr("style","height:"+ lastChildHeight +"px");
												$(".ui-drop_dialog_content").attr("style","height:"+ lastChildHeight +"px");
												$(".border.lft").attr("style","height:"+ lastChildHeight +"px");
												$(".border.rit").attr("style","height:"+ lastChildHeight +"px");
												$("#sel_orgc").attr("style","height:"+ lastChildHeight +"px");																																												
										   });
										   var labelProd = $("<label />").html(checkOrgName)
										   var div = $("<div></div>").attr("id", checkOrgPk).attr("name", checkOrgPk).attr("style", "float:left;display:inline").append(checkBox).append(labelProd);
										   $("#sel_orgc").append(div);
										}else{					  
										   $("#"+checkOrgPk).remove();
										}
										var allChild = $("#sel_orgc").children();
										var lastChildHeight = allChild[allChild.length - 1].offsetTop;
										offsetTop=lastChildHeight;											
										$(".ui-drop_dialog_content").attr("style","height:"+ lastChildHeight +"px");
						                $(".border.lft").attr("style","height:"+ lastChildHeight +"px");
						                $(".border.rit").attr("style","height:"+ lastChildHeight +"px");
						                $("#sel_orgc").attr("style","height:"+ lastChildHeight +"px");							
										
									})									
									//鏌ヨ璇ユ槸鍚﹀凡缁忚閫夋嫨
									var objOrg = $("#"+obj.pk_COMPLAINTTYPE);
									if(objOrg.length > 0){
										inputObj.attr("checked", "checked");
									}
									var label = $("<label></label>").text(obj.vname);
									var inputObj1 = $("<input/>").val(obj.pk_complainttype).attr(
											"type", "hidden");
									var inputObj2 = $("<input/>").val(obj.pk_group).attr(
											"type", "hidden");	
									var div = $("<div></div>")
											.addClass("tree_category12")
											.append(inputObj).append(label).append(inputObj1);				
																																							
									$("#complainHeadPicker").find(".tree_list12")
											.append(div);
								}									
						  }
					   }
					}		
				})
				var position = $(this).offset();
				position.top += 25;
				$("#complainHeadPicker").slideDown("normal");	
				$("#complainHeadPicker").offset(position);										
			});
	
	$(".tree_btns122 .idBtnPicked").live('click', function() {
		var items = $("#complainHeadPicker").find("[name='pk_purOrg_resp']");
		if(items != null){
			var str="";
			var pks="";
			for(var loop=0; loop<items.length; loop++){
				if(items[loop].checked == true){					
					str += $(items[loop]).next("label").html()+ "," ;
					pks += $(items[loop]).val().substr(0,20)+ "," ;	
				}
			}
			var length1=str.length;
			var length2=pks.length;
			$("#qryInvoiceNo").val(str.substring(0, length1-1));			
			$("#pk_purInvoice").val(pks.substring(0, length2-1));
			$("#offsetTop").val(offsetTop);
		}
		if(str != ""){
			$("#qryInvoiceNo").val(str.substring(0, length1-1)).attr("style", "color:black");			
		}
		$("#complainHeadPicker").slideUp();
		return 0;
	});
	
	$(".tree_btns122 .idBtnCancel").live('click', function(){
		$("#org_filterc").val("");
		$("#complainHeadPicker").slideUp();
	})
	
	function len(s) { 
		var l = 0; 
		var a = s.split(""); 
		for (var i=0;i<a.length;i++) { 
			if (a[i].charCodeAt(0)<299) { 
				l++; 
			} else { 
				l+=2; 
			} 
		} 
		return l; 
	}
	
	this.init = function() {
	}
	
}

$(function() {
	var appealOrg = new AppealOrgList();
	appealOrg.init();
});

